﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FCS.Communication;
using System.Net;
using System.IO;
using System.Threading;
using FCS.GlobalComponents;

namespace FCS.Testing
{
    class QueueTester
    {
        /// <summary>
        /// This console application tests to see if the queue is working properly
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string ipIn;
            IPAddress ip;
            int local, remote;

            // Get the inital connection 
            Console.WriteLine("Command Queue Tester");
            Console.WriteLine("This will test the connection to the cRIO and responce");
            Console.WriteLine("*******************************************************");
            Console.WriteLine("C = cRIO Configuration; E = Emulator Configuration; M = Manual Configuration");
            Console.Write("Configure network? (C/E/M): ");
            char configNetwork = Convert.ToChar(Console.ReadLine().ToUpper());

            CommandPriorityQueue cpq;
            if (configNetwork == 'C')
            {
                cpq = new CommandPriorityQueue();
                cpq.SetDefaultCRIOAddress("test");
            }
            else
            {
                if (configNetwork == 'E')
                {
                    //Console.Write("Enter the ip address: ");
                    String strHostName = Dns.GetHostName();
                    IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
                    IPAddress[] addr = ipEntry.AddressList;

                    Console.WriteLine("Select IP");
                    for (int i = 0; i < addr.Length; i++)
                        Console.WriteLine(i + ": " + addr[i].ToString());

                    

                    ip = addr[Convert.ToInt32(Console.ReadLine())];
                    local = 50001;
                    remote = 50000;

                    Console.WriteLine("IP Address: " + ip);
                }
                else
                {
                    Console.Write("Enter the ip address: ");
                    ipIn = Console.ReadLine();
                    ip = IPAddress.Parse(ipIn);
                    Console.Write("Enter local port: ");
                    local = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter remote port: ");
                    remote = Convert.ToInt32(Console.ReadLine());
                }
                cpq = new CommandPriorityQueue(ip, local, remote);
            }


            Console.WriteLine();

            Console.Write("Manual or file: ");
            string mode = Console.ReadLine();

            if (mode.Length == 0)
            {
                try
                {
                    while (true)
                    {
                        Console.Write("Enter in commands: ");
                        string input = Console.ReadLine();
                        string[] cmd = input.Split(' ');

                        List<string> cmd2 = new List<string>();
                        foreach (string s in cmd)
                        {
                            cmd2.Add(s);
                        }

                        Console.Write("Enter priority (1-lomac, 2-kiosk, 3-server, 4-reset: ");
                        int choice = Convert.ToInt32(Console.ReadLine());

                        CommandPriority c;
                        if (choice == 1)
                            c = CommandPriority.lmoc;
                        else if (choice == 2)
                            c = CommandPriority.kiosk;
                        else
                            c = CommandPriority.patterns;
                       
                        if (choice != 4)
                        {
                            Console.Write("Enter state: ");
                            bool commandSent = cpq.SendCommand(Console.ReadLine(), cmd2, c, "test");
                            if (commandSent)
                                Console.WriteLine(input + ": " + choice);
                        }
                        else
                        {
                            Console.WriteLine("Resetting queue");
                            cpq.ResetThreshold(CommandPriority.lmoc, "test");
                        }

                        cRIOState state = cpq.GetResources();

                        Console.WriteLine("Response: " + state.BollardStates[0]);
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
            }
            else
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader(mode);
                    while (!sr.EndOfStream)
                    {
                        string input = sr.ReadLine();
                        string[] cmd = input.Split(' ');

                        List<string> cmd2 = new List<string>();
                        foreach (string s in cmd)
                        {
                            cmd2.Add(s);
                        }

                        int choice = Convert.ToInt32(sr.ReadLine());

                        CommandPriority c;
                        if (choice == 1)
                            c = CommandPriority.lmoc;
                        else if (choice == 2)
                            c = CommandPriority.kiosk;
                        else
                            c = CommandPriority.patterns;

                        if (choice != 4)
                        {
                            Console.WriteLine("Sending command with priority " + c);
                            bool commandSent = cpq.SendValveCommand("on", cmd2, c);
                            if (commandSent)
                                Console.WriteLine(input + ": " + choice);
                        }
                        else
                        {
                            Console.WriteLine("Resetting queue");
                            cpq.ResetThreshold(CommandPriority.lmoc, "test");
                        }

                        cRIOState state = cpq.GetResources();

                        Console.WriteLine("Response: " + state.BollardStates[0]);

                        Console.ReadKey();
                    }
                }
                catch (ArgumentNullException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                catch (FileNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                catch (DirectoryNotFoundException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                        sr.Dispose();
                    }
                }

                Console.Read();
            }
        }
    }
}
